package com.maven.rabbitmq.direct;

import com.maven.rabbitmq.constant.Constant;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class DirectProduce {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1. 建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.135.54.49");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("rabbitmq");

        Connection connection = connectionFactory.newConnection();
        //2. 建立信道
        Channel channel = connection.createChannel();

        //3. 声明 队列
        channel.queueDeclare(Constant.DIRECT_QUEUE_1,true,false,true,null);
        channel.queueDeclare(Constant.DIRECT_QUEUE_2,true,false,true,null);

        // 4. 声明交换机
        channel.exchangeDeclare(Constant.DIRECT_EXCHANGE_1, BuiltinExchangeType.DIRECT,true);
        channel.exchangeDeclare(Constant.DIRECT_EXCHANGE_2, BuiltinExchangeType.DIRECT,true);

        //5. 交换机与队列的绑定
        channel.queueBind(Constant.DIRECT_QUEUE_1,Constant.DIRECT_EXCHANGE_1,"a");
        channel.queueBind(Constant.DIRECT_QUEUE_2,Constant.DIRECT_EXCHANGE_1,"a");
        channel.queueBind(Constant.DIRECT_QUEUE_2,Constant.DIRECT_EXCHANGE_2,"b");


        //6. 发送信息
        channel.basicPublish(Constant.DIRECT_EXCHANGE_1,"a",null,"我是 direct queue : a ".getBytes());
        channel.basicPublish(Constant.DIRECT_EXCHANGE_2,"b",null,"我是 direct queue : b ".getBytes());


        channel.close();
        connection.close();
    }
}
